﻿// Activities tab

function populateActivities()
{
    if (!tab_networking_loaded)
        show_wait("networking_tab");
    osapi.http.get({
        'href': rest_activities_friends,
        'format': 'json',
        'oauth_service_name': oauth_service_name,
        'authz': authz_type
    }).execute(fill_activities);
}

function fill_activities(data)
{
    show_errors_if_any(data);
    //saving hash in order to know when new data arrives, 
    //if the hash is the same when the tab is refreshed - no need to redraw it
    if (compare_and_save_hash(data.metadata.DataHash))
        return;
    show_wait("networking_tab");

    var tab_container = $("networking_tab_container");

    remove_all_children(tab_container);

    // container for retrieved data
    var container = new Element('div');
    container.addClassName('contentbox');
    tab_container.appendChild(container);

    var i = 0;
    //temp value used to avoid repeating the same date in the list
    var _temp_date_point;
    while (i < data.content.totalResults)
    {
        if (data.content.entry[i])
        {

            activity = data.content.entry[i];
            var _full_date = activity.postedTime.split(' ')[0];
            var _full_time = activity.postedTime.split(' ')[1];
            var _year = _full_date.split('-')[0];
            var _month = _full_date.split('-')[1];
            var _date = _full_date.split('-')[2];
            var _hours = _full_time.split(':')[0]
            var _minutes = _full_time.split(':')[1]

            _full_date = new Date();
            _full_date.setDate(_date);
            _full_date.setMonth(_month - 1);
            _full_date.setYear(_year);
            _full_date.setHours(_hours);
            _full_date.setMinutes(_minutes);

            var day_to_display = _full_date.format('F j');
            if (day_to_display == (new Date).format('F j'))
                day_to_display = 'Today';
            if (_full_date.format('Y') == (new Date).format('Y') && _full_date.format('z') == (new Date).format('z') - 1)
                day_to_display = 'Yesterday';

            var time_to_display = _full_date.format('H:i');
            //var _time = activity.postedTime.split(' ')[1];

            var item = new Element('div');
            item.addClassName('activity_mini');
            container.appendChild(item);

            var details = new Element('div');

            var date_day = new Element('div');
            date_day.addClassName('date_day');
            date_day.update(day_to_display);

            var date_time = new Element('span');
            date_time.addClassName('nobreak');
            date_time.addClassName('date_time');
            date_time.update(time_to_display);

            details.appendChild(add_person_container(activity.userId, activity.id));
            details.innerHTML += '&nbsp;' + activity.title + '&nbsp;';
            details.appendChild(date_time);

            if (_temp_date_point != separate_date_from_datetime(activity.postedTime))
                item.appendChild(date_day);
            else
                item.innerHTML += "<div style='clear:both;'></div>";
            item.appendChild(details);

            populate_activity_person_name(activity);
            _temp_date_point = separate_date_from_datetime(data.content.entry[i].postedTime);
        }
        i++;
    }
    hide_wait("networking_tab");
    tab_networking_loaded = true;
}

function populate_activity_person_name(activity)
{
    osapi.http.get({
        'href': hostname + "/social/rest/people/" + activity.userId + "/@self?alt=json",
        'format': 'json',
        'oauth_service_name': oauth_service_name,
        'authz': authz_type
    }).execute(function (data)
    {
        show_errors_if_any(data);
        update_person_container(activity.userId, activity.id, data.content.entry.displayName, get_profile_url(data.content.entry.id));
    });

}

function update_activities()
{
    $('update_image').setAttribute('src', update_image_moving_url);
    populateActivities();
    $('update_image').setAttribute('src', update_image_url);
}
